package com.rf.richfitwheel.workflow.controller;

import com.rf.richfitwheel.admin.annotation.UserOperateLog;
import com.rf.richfitwheel.admin.sys.model.OrgHasParent;
import com.rf.richfitwheel.admin.sys.model.OrgVO;
import com.rf.richfitwheel.admin.sys.vo.UserVO;
import com.rf.richfitwheel.admin.utils.UserUtil;
import com.rf.richfitwheel.common.utils.R;
import com.rf.richfitwheel.workflow.feignclient.SysOrgFeignService;
import com.rf.richfitwheel.workflow.model.WfStationProcess;
import com.rf.richfitwheel.workflow.model.WfStationProcessOrder;
import com.rf.richfitwheel.workflow.model.WfStationProcessParPub;
import com.rf.richfitwheel.workflow.service.WfAuthRuleService;
import com.rf.richfitwheel.workflow.service.WfStationProcessOrderService;
import com.rf.richfitwheel.workflow.service.WfStationProcessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 岗位流程
 *
 * @author zhanglu
 * @email zhanglu2@cnpc.com.cn
 * @date 2021-07-22 13:57:22
 */
@RestController
@RequestMapping("/wfstationprocess")
public class WfStationProcessController {

    protected static final Logger log = LoggerFactory.getLogger(WfStationProcessController.class);

    @Autowired
    private WfStationProcessService wfStationProcessService;
    @Autowired
    private WfStationProcessOrderService wfStationProcessOrderService;
    
    @Autowired
    private SysOrgFeignService sysOrgFeignService;
    @Autowired
    private WfAuthRuleService wfAuthRuleService;
    /**
     * 岗位分页查询
     *
     * @param orgCode
     * @param folderCode
     * @return
     */
    @GetMapping("/queryWfStationProcessListByOrgCodeAndFolderCode")
    public R queryWfStationProcessListByOrgCodeAndFolderCode(String orgCode, String folderCode) {
        OrgVO orgVO = sysOrgFeignService.getOrgVOObjectByOrgCode(orgCode);
    
        UserVO userVO = UserUtil.getUser();
        //wfStationProcessService.initWfStationProcess(orgCode, userVO);
        R r = R.ok();
        WfStationProcess wfStationProcess = wfStationProcessService.selectByOrgCodeAndFolderCode(orgCode, folderCode);
        r.put("wfStationProcess", wfStationProcess);
        if (!ObjectUtils.isEmpty(wfStationProcess)) {
            List<WfStationProcessParPub> parSubList = wfStationProcessService.getWfStationProcessParPubByStationProcessId(wfStationProcess.getId());
            r.put("parSubList", parSubList);
            List<WfStationProcessOrder> wfStationProcessOrders = wfStationProcessOrderService.selectListByStationProcessId(wfStationProcess.getId());
            r.put("wfStationProcessOrders", wfStationProcessOrders);
        }else{
            r.put("parSubList", new ArrayList<>());
        }
        //上级是否参与的标志
        r.put("parSubCon", false);
        if(orgVO != null && orgVO.getParentOrg() != null){
            WfStationProcess wfStationProcessParent = wfStationProcessService.selectByOrgCodeAndFolderCode(orgVO.getParentOrg().getOrgCode(), folderCode);
            if(wfStationProcessParent != null){
                if("1".equals(wfStationProcessParent.getParSub()) || "2".equals(wfStationProcessParent.getParSub())){
                    //设置了有条件参与或无条件参与
                    r.put("parSubCon", true);
                }
            }
        }
        return r;
    }


    //保存并发布流程
    @PostMapping("/saveAndPublishWfStationProcess")
    @UserOperateLog(module = "工作流", methods = "saveAndPublishWfStationProcess", description = "保存并发布流程", logType = "2")
    public R saveAndPublishWfStationProcess(@RequestBody String param) {
        UserVO userVO = UserUtil.getUser();
        try {
            wfStationProcessService.saveAndPublishWfStationProcess(param, userVO);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error("岗位流程发布失败");
        }
        return R.ok();
    }
    
    /**
     * 通过当前机构编码查询上级给设置的流程岗位,上级没设置或不参与的继续向上查找直至顶级
     * @param orgCode 当前机构
     * @param folderCode 流程目录编码
     * @return 流程岗位配置
     */
    @GetMapping("/getParentStationOrders/{orgCode}/{folderCode}")
    public R getParentStationOrders(@PathVariable("orgCode") String orgCode, @PathVariable("folderCode") String folderCode){
        try{
            OrgVO orgVO = sysOrgFeignService.getOrgVOObjectByOrgCode(orgCode);
            OrgHasParent orgHasParent = sysOrgFeignService.getOrgListByOrgIdUpHasNoSelfWithTree(orgVO.getId());
            List<WfStationProcessOrder> listParent = wfStationProcessService.getParentStationOrders(orgHasParent, folderCode);
        
            /*if(orgVO == null || orgVO.getParentOrg() == null){
                log.warn("无法找到上级机构");
                return R.ok().put("data", new ArrayList<WfStationProcessOrder>());
            }
            WfStationProcess wfStationProcess = wfStationProcessService.selectByOrgCodeAndFolderCode(orgVO.getParentOrg().getOrgCode(), folderCode);
            if(wfStationProcess == null){
                log.info("上级机构没有设置流程");
                return R.ok().put("data", new ArrayList<WfStationProcessOrder>());
            }
            if("3".equals(wfStationProcess.getParSub()) || StringUtils.isBlank(wfStationProcess.getParSub())){
                //上级如果没设置或者设置了不参与下级审批的情况下就直接返回空的了
                return R.ok().put("data", new ArrayList<WfStationProcessOrder>());
            }
            List<WfStationProcessOrder> wfStationProcessOrders = wfStationProcessOrderService.selectListByStationProcessId(wfStationProcess.getId());
            List<WfStationProcessOrder> listParent = wfStationProcessOrders.stream().peek(m -> {
                List<WfAuthRule> authRules = wfAuthRuleService.listByStationProcessOrderId(m.getId());
                m.setAuthRules(authRules);
                m.setAuthRuleCount(authRules.size());
                m.setAuthRuleLoaded(true);
                m.setId(null);
                m.setSourceFrom("1");
            }).collect(Collectors.toList());*/
            return R.ok().put("data", listParent);
        }catch(Exception e){
            log.error("没找到上级机构", e);
            return R.ok();
        }
    }
}
